import { _decorator, Component, Node, Vec3, v3, Camera } from 'cc';
import SNStateSyncAction, { FilterSyncDataEnum } from '../../ngame/actions/statesync/SNStateSyncAction';
import NSyncServer from '../ngame/sync/NSyncServer';
const { ccclass, property } = _decorator;

@ccclass('ICameraController')
export class ICameraController extends Component {

    @property(Node)
    target:Node = null;

    //是否看向中间
    isCenter:boolean = false;
    
    private offset:Vec3 = v3();
    private vec3:Vec3 = v3();

    start() {

        this.schedule(this.initSyncData,.5)

    }

    initSyncData(){

        //获取相机中间
        if(NSyncServer.getIns().info && (!NSyncServer.getIns().info.isServer)){
            SNStateSyncAction.nFilterSyncData({
                type:FilterSyncDataEnum.D3_RANGE,
                isClean:true,
                x:this.node.worldPosition.x,
                y:this.node.worldPosition.y,
                z:this.node.worldPosition.z,
                range:10
            });
        }

    }

    update (deltaTime: number) {
        if(this.target){

            if(this.isCenter){
                Vec3.add(this.vec3,this.offset,v3());
            }else{
                Vec3.add(this.vec3,this.offset,this.target.position);
            }
            this.node.setPosition(this.node.position.clone().lerp(this.vec3,deltaTime * 2));

        }
    }
}

